package www.ym.com.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import www.ym.com.common.JacksonObjectMapper;

import java.util.List;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

//    @Value("${file.baseImagePath}")
//    private String baseImagePath;

    /**
     * 扩展mvc的小心转换器
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转换为Json
        converter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器对象追加到mvc框架的转换器集合中
        converters.add(0,converter);
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始静态资源映射");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
        //上传图片的服务器路径
//        registry.addResourceHandler("/file/**").addResourceLocations("file:"+baseImagePath);

    }

//        @Override
//        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//            for (int i = converters.size() - 1; i >= 0; i--) {
//                if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) {
//                    converters.remove(i);
//                }
//            }
//
//            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//            FastJsonConfig fastJsonConfig = new FastJsonConfig();
//            fastJsonConfig.setSerializerFeatures(
//                    SerializerFeature.WriteMapNullValue,//是否输出值为null的字段
//                    SerializerFeature.WriteNullListAsEmpty,//List字段如果为null,输出为[]
//                    SerializerFeature.WriteNullStringAsEmpty,//字符类型字段如果为null,输出为"",而非null
//                    SerializerFeature.WriteNullNumberAsZero,//数值字段如果为null,输出为0,而非null
//                    SerializerFeature.WriteNullBooleanAsFalse//Boolean字段如果为null,输出为false,而非null
//            );
//            fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
//            fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//            List<MediaType> fastMediaTypes = new ArrayList<>();
//            fastMediaTypes.add(MediaType.APPLICATION_JSON);
//            fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
//            converters.add(fastJsonHttpMessageConverter);
//        }

//        @Override
//        public void addCorsMappings(CorsRegistry registry) {
            // 设置允许跨域的路径
//            registry.addMapping("/**")
//                    // 设置允许跨域请求的域名
//                    .allowedOrigins("*")
//                    // 是否允许证书 不再默认开启
//                    .allowCredentials(true)
//                    // 设置允许的方法
//                    .allowedMethods("*")
//                    // 允许任意头
//                    .allowedHeaders("*")
//                    // 跨域允许时间
//                    .maxAge(3600);
//        }
//        private CorsConfiguration corsConfig() {
//            CorsConfiguration corsConfiguration = new CorsConfiguration();
//            /* 请求常用的三种配置，*代表允许所有，当时你也可以自定义属性（比如header只能带什么，只能是post方式等等）
//             */
//            corsConfiguration.addAllowedOrigin("*");
//            corsConfiguration.addAllowedHeader("*");
//            corsConfiguration.addAllowedMethod("*");
//            corsConfiguration.setAllowCredentials(true);
//            corsConfiguration.setMaxAge(3600L);
//            return corsConfiguration;
//        }
//        @Bean
//        public CorsFilter corsFilter() {
//            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//            source.registerCorsConfiguration("/**", corsConfig());
//            return new CorsFilter(source);
//        }

    }



